package com.karl.mytask_2.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.karl.mytask_2.R;
import com.karl.mytask_2.bean.MyUser;
import com.karl.mytask_2.tools.BaseActivity;

import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Karl on 2016/5/21.
 */
public class UpdatePasswordActivity extends BaseActivity implements View.OnClickListener {
    private TextView mToolBarTitle;
    private ImageButton mBack;
    private Button mUpdate;
    private EditText mOldPass,mNewPass,mNewPass2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.update_password);
        initView();
        initEvent();
            

    }
    private void initView(){
        mToolBarTitle = (TextView) findViewById(R.id.tool_bar2_title);
        mBack = (ImageButton) findViewById(R.id.tool_bar2_back);
        mUpdate = (Button) findViewById(R.id.update_btn);
        mOldPass = (EditText) findViewById(R.id.update_old_password);
        mNewPass = (EditText) findViewById(R.id.update_new_password);
        mNewPass2 = (EditText) findViewById(R.id.update_new_password2);
    }
    private void initEvent(){
        mToolBarTitle.setText("更改密码");
        mBack.setOnClickListener(this);
        mUpdate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.update_btn:
                Toast.makeText(UpdatePasswordActivity.this, "提交中，请稍等...", Toast.LENGTH_SHORT).show();
                updatePass();
                break;
            case R.id.tool_bar_btn:
                UpdatePasswordActivity.this.finish();
                break;
        }
    }
    
    private void updatePass(){
        MyUser user = new MyUser();
        String oldPass = mOldPass.getText().toString();
        String newPass = mNewPass.getText().toString();
        String newPass2 = mNewPass2.getText().toString();
        if (!(oldPass.equals("")||newPass.equals("")|| newPass2.equals(""))) {
            if (newPass.equals(newPass2)) {
                user.updateCurrentUserPassword(UpdatePasswordActivity.this, oldPass, newPass, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(UpdatePasswordActivity.this, "修改密码成功！", Toast.LENGTH_SHORT).show();
                        UpdatePasswordActivity.this.finish();
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        Toast.makeText(UpdatePasswordActivity.this, "修改密码失败，请检查网络或者重新尝试！", Toast.LENGTH_SHORT).show();

                    }
                });
            } else {
                Toast.makeText(UpdatePasswordActivity.this, "两次新密码输入不一致！", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(UpdatePasswordActivity.this, "输入不能为空！", Toast.LENGTH_SHORT).show();
        }
    }
}
